package com.glory.wxelectricitypda.fragment;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.activity.PackPackageActivity;
import com.glory.wxelectricitypda.adapter.PackListAdapter;
import com.glory.wxelectricitypda.base.Constant;
import com.glory.wxelectricitypda.bean.BaseResponseBean;
import com.glory.wxelectricitypda.bean.PackBean;
import com.glory.wxelectricitypda.bean.ResponseAfterPackBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AlreadyDoneFragment extends FreeUI_BaseFragment {

    public PackListAdapter packListAdapter;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.llMain)
    FrameLayout llMain;
    @BindView(R.id.checkAll)
    CheckBox checkAll;

    public AlreadyDoneFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_already_done;
    }

    @Override
    protected void initView() {
        rvThingList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(getActivity(), 1));

        packListAdapter = new PackListAdapter(null);
        packListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        packListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(packListAdapter);
    }

    @Override
    protected void initData() {
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                packListAdapter.setAll(isChecked);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }


    @OnClick({R.id.tvPack, R.id.tvPrint})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvPack:
                final List<PackBean> selectList = new ArrayList<>();
                for (PackBean packBean : packListAdapter.getData()) {
                    if (packBean.isSelect()) {
                        selectList.add(packBean);
                    }
                }
                if (selectList.size() == 0) {
                    showToast("请选择要拆包的批次", false);
                    return;
                }
//                if (selectList.size() > 10) {
//                    showToast("最多只能选择10条批次", false);
//                    return;
//                }
                JSONObject jsonObject = new JSONObject();
                JSONObject jsonObject1 = new JSONObject();
                try {
                    jsonObject1.put("ACTIONTYPE", "UnPack");
                    jsonObject1.put("PACKAGETYPE", ((PackPackageActivity) getActivity()).packType
                            .equalsIgnoreCase("BoxPackageType") ? "BoxPackageType" : "SmallBoxPackageType");
                    JSONArray jsonArray = new JSONArray(new Gson().toJson(selectList));
                    jsonObject1.put("LOTLIST", jsonArray);
                    jsonObject1.put("PACKEDLOTID", "");
                    jsonObject.put("Data",jsonObject1);
                    jsonObject.put("MessageName","EUIT.EUITPACKING");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostMesForwardUtils(jsonObject, this,new FreeUI_DialogEntityCallBack<ResponseBean<ResponseAfterPackBean>>(
                        new TypeToken<ResponseBean<ResponseAfterPackBean>>() {
                        }.getType(), getFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<ResponseAfterPackBean>> response) {
                        if (response.body().isSuccess(getContext(), true, getActivity().getSupportFragmentManager())) {
                            packListAdapter.getData().removeAll(selectList);
                            packListAdapter.clearPos();
                            packListAdapter.notifyDataSetChanged();
                            ((PackPackageActivity) getActivity()).setNotDoneList(response.body().getBody().getREMAINLOTLIST());
                            showToast("拆包成功", true);
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<ResponseAfterPackBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), "拆包请求");
                    }
                });
                break;
            case R.id.tvPrint:
                final List<PackBean> selectList2 = new ArrayList<>();
                for (PackBean packBean : packListAdapter.getData()) {
                    if (packBean.isSelect()) {
                        selectList2.add(packBean);
                    }
                }
                if (selectList2.size() == 0) {
                    showToast("请选择要打印的批次", false);
                    return;
                }
//                if (selectList2.size() > 5) {
//                    showToast("最多只能选择5条批次", false);
//                    return;
//                }
                JSONObject jsonObject2 = new JSONObject();
                try {
                    jsonObject2.put("PACKAGETYPE", ((PackPackageActivity) getActivity()).packType
                            .equalsIgnoreCase("BoxPackageType") ? "BOX" : "SMALLBOX");
                    JSONArray jsonArray = new JSONArray(new Gson().toJson(selectList2));
                    jsonObject2.put("LOTLIST", jsonArray);
                    jsonObject2.put("CLIENTIP", Constant.PRINT_IP);
                    jsonObject2.put("CLIENTIP2", Constant.PRINT_IP2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestWithNormalCallBack("EUIT.EUITPRINT", jsonObject2, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                        new TypeToken<BaseResponseBean<?>>() {
                        }.getType(), getFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<?>> response) {
                        if (response.body().isSuccess(getContext(), false)) {
                            showToast("打印成功", true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    getActivity().finish();
                                }
                            }, 2000);
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<?>> response) {
                        super.onError(response);
                        loadError(response.getException(), "打印请求");
                    }
                });
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            ((PackPackageActivity) getActivity()).etLotId.setEnabled(false);
        }
    }
}
